//
//  BBSCell.m
//  网易新闻
//
//  Created by dllo on 15/10/9.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "BBSCell.h"
#import "SDWebImage/UIImageView+WebCache.h"
#import "Settings.h"
@interface BBSCell()
@property(nonatomic, retain)UIImageView *img;
@property(nonatomic, retain)UILabel *title;
@property(nonatomic, retain)UILabel *name;
@property(nonatomic, retain)UILabel *desc;
@property(nonatomic, retain)UILabel *v;
@end
@implementation BBSCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        
        self.img = [[UIImageView alloc]init];
        self.img.backgroundColor = [UIColor lightGrayColor];
        self.img.layer.masksToBounds = YES;
        self.img.layer.cornerRadius = 15;
        [self.contentView addSubview:self.img];
        [self.img release];
        
        self.title = [[UILabel alloc]init];
        self.title.textColor = [UIColor blueColor];
        self.title.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.title];
        [self.title release];
        
        self.name = [[UILabel alloc]init];
        self.name.textColor = [UIColor lightGrayColor];
        self.name.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.name];
        [self.name release];
        
        self.desc = [[UILabel alloc]init];
        self.desc.numberOfLines = 0;
        self.desc.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.contentView addSubview:self.desc];
        [self.desc release];
        
        self.v = [[UILabel alloc]init];
        self.v.textColor = [UIColor lightGrayColor];
        self.v.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.v];
        [self.v release];
        
        [self notificationHandle];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.img.frame = CGRectMake(10, 15, 30, 30);
    self.title.frame = CGRectMake(50, 15, 260, 15);
    self.name.frame = CGRectMake(50, 35, 300, 15);
    self.v.frame = CGRectMake(310, 15, 65, 15);
}

- (void)setMod:(BBSModel *)mod{
    if (_mod != mod) {
        [_mod release];
        _mod = [mod retain];
    }
    [self.img sd_setImageWithURL:self.mod.timg placeholderImage:nil];
    
    if (self.mod.n) {
        self.title.text = self.mod.n;
    }else{
        self.title.text = @"火星网友";
    }
    if (!self.mod.t) {
        self.mod.t = @"";
    }
    NSRange range = [self.mod.f rangeOfString:@"网友"];
    self.name.text = [NSString stringWithFormat:@"%@ %@",[self.mod.f substringToIndex:range.location+range.length], self.mod.t];
    CGRect rect = [self.mod.b boundingRectWithSize:CGSizeMake(300, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    self.desc.frame = CGRectMake(50, 55, 300, rect.size.height+1);
    self.desc.text = self.mod.b;
    self.v.text = [NSString stringWithFormat:@"%lu赞", self.mod.v];
    self.height = 65+rect.size.height;
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.desc.textColor = [[Settings shareSettings]color:BlackToWhite];
    }];
}

- (void)dealloc{
    [_title release];
    [_name release];
    [_desc release];
    [_img release];
    [super dealloc];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
